<template>
  <div class="table-wrap">
    <div class="search_header">
      <span>
        <el-button
          icon="el-icon-plus"
          @click="addProject"
          v-has="'messageManagement:add'"
          >新增项目</el-button
        >
      </span>
      <div class="search_header_right">
        <el-select
          placeholder="项目分类"
          v-model="parameter.projectType"
          clearable
          @change="search"
        >
          <el-option
            v-for="item in projectTypeOptions"
            :key="item.dictValue"
            :label="item.dictText"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
        <el-select
          placeholder="四大结构调整"
          v-model="parameter.projectSdjg"
          clearable
          @change="search"
        >
          <el-option
            v-for="item in projectStructOptions"
            :key="item.dictValue"
            :label="item.dictText"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
        <el-select
          placeholder="细分类型"
          v-model="parameter.xflx"
          clearable
          @change="search"
        >
          <el-option
            v-for="item in projectDivisionOptions"
            :key="item.dictValue"
            :label="item.dictText"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
        <el-select
          placeholder="项目状态"
          v-model="parameter.projectStatus"
          clearable
          @change="search"
        >
          <el-option
            v-for="item in projectStatusOptions"
            :key="item.dictValue"
            :label="item.dictText"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
        <el-select
          placeholder="审核状态"
          v-model="parameter.bpmStatus"
          clearable
          @change="search"
        >
          <el-option
            v-for="item in projectCheckOptions"
            :key="item.dictValue"
            :label="item.dictText"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
        <el-input
          placeholder="  请输入项目名称、业主单位"
          v-model="parameter.keyword"
          clearable
          @keydown.enter.native="search"
        >
          <span
            @click="search"
            slot="suffix"
            style="display: inline-block"
          >
            <Icon
              title="搜索"
              name="sousuo"
              color="#A7ADB5"
              font-size="20"
            />
          </span>
        </el-input>
      </div>
    </div>
    <el-table
      ref="tableRef"
      :data="projectList"
      :header-cell-style="{
        background: '#F2F6FB ',
        color: ' #6B767D'
      }"
      :row-class-name="tableRowClassName"
      @sort-change="handleSort"
    >
      <el-table-column
        label="序号"
        align="center"
        show-overflow-tooltip
        width="80"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="项目编号"
        prop="projectNo"
        show-overflow-tooltip
        sortable="custom"
      />
      <el-table-column
        label="项目分类"
        prop="projectTypeText"
        show-overflow-tooltip
      />
      <el-table-column
        label="项目名称"
        prop="projectName"
        show-overflow-tooltip
      />
      <el-table-column
        label="四大结构调整"
        prop="projectSdjgText"
        show-overflow-tooltip
      />
      <el-table-column
        label="细分类型"
        prop="xflxText"
        show-overflow-tooltip
      />
      <el-table-column
        label="建设规模"
        prop="jsgm"
        show-overflow-tooltip
        width="150"
      />
      <el-table-column
        label="建设周期"
        prop="jszqText"
        show-overflow-tooltip
      />
      <el-table-column
        label="项目状态"
        prop="projectStatusText"
        show-overflow-tooltip
      />
      <el-table-column
        label="审核状态"
        prop="bpmStatusText"
        show-overflow-tooltip
      />
      <el-table-column
        label="更新时间"
        prop="updateTime"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        label="操作"
        show-overflow-tooltip
        min-width="160"
      >
        <template slot-scope="scope">
          <el-button
            v-if="
              !isAdmin &&
              scope.row.bpmStatus !== '2' &&
              scope.row.projectStatus !== '1' &&
              $filterGlobalPermission(
                'messageManagement:submit'
              )
            "
            @click="updateProgress(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            >填报</el-button
          >
          <el-button
            v-if="
              scope.row.bpmStatus === '2' &&
              $filterGlobalPermission(
                'messageManagement:check'
              )
            "
            @click="checkProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            >审核</el-button
          >
          <el-button
            v-if="
              scope.row.bpmStatus === '3' &&
              $filterGlobalPermission(
                'messageManagement:back'
              )
            "
            @click="backProject(scope.row)"
            type="text"
            style="color: #ff5b5b; font-weight: 400"
            >退回</el-button
          >
          <el-button
            v-if="
              scope.row.bpmStatus !== '2' &&
              $filterGlobalPermission(
                'messageManagement:edit'
              )
            "
            @click="updateProgress(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            >编辑</el-button
          >
          <el-button
            @click="viewProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            >查看</el-button
          >
          <el-button
            @click="deleteProject(scope.row)"
            type="text"
            style="color: #ff5b5b; font-weight: 400"
            v-has="'messageManagement:delete'"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination" v-if="total">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="parameter.pageSize"
        @current-change="pageChange"
        :total="total"
      >
      </el-pagination>
    </div>
    <tuihui ref="tuihui" @forceRender="getList"></tuihui>
  </div>
</template>

<script>
import {
  queryProjectList,
  deleteProjectInfo,
  queryJudgeExist
} from '@/api/projectMessage'
import { queryOptions } from '@/api/dict'
import Icon from '@/components/icon'
import { create } from 'sortablejs'
import { isAdmin, isEnterprise } from '@/utils/permission'
import tuihui from './tuihui.vue'

export default {
  name: 'index',
  components: {
    Icon,
    tuihui
  },
  data() {
    return {
      projectList: [],
      total: '',
      parameter: {
        projectType: '',
        projectSdjg: '',
        xflx: '',
        projectStatus: '',
        bpmStatus: '',
        keyword: '',
        sortFields: '',
        sortTypes: '',
        pageIndex: 1,
        pageSize: 10
      },
      roleOptions: [],
      projectTypeOptions: [], // 项目分类
      projectStructOptions: [], // 四大结构调整
      projectDivisionOptions: [], // 细分类型
      projectCheckOptions: [], // 审核状态
      projectStatusOptions: [] // 项目状态
    }
  },
  computed: {
    isAdmin() {
      return isAdmin()
    },
    isEnterprise() {
      return isEnterprise()
    }
  },
  mounted() {
    this.getAllOptions()
    this.getList()
  },
  methods: {
    // 所有选项
    getAllOptions() {
      queryOptions(
        'projectType,projectSdjg,xflx,projectStatus,bpmStatus'
      ).then((res) => {
        if (res.success) {
          // 项目分类
          this.projectTypeOptions = res.data.filter(
            (i) => i.dictCode === 'projectType'
          )[0].resourceDictItems

          // 四大结构调整
          this.projectStructOptions = res.data.filter(
            (i) => i.dictCode === 'projectSdjg'
          )[0].resourceDictItems

          // 细分类型
          this.projectDivisionOptions = res.data.filter(
            (i) => i.dictCode === 'xflx'
          )[0].resourceDictItems

          // 项目状态
          this.projectStatusOptions = res.data.filter(
            (i) => i.dictCode === 'projectStatus'
          )[0].resourceDictItems

          // 审核状态
          this.projectCheckOptions = res.data.filter(
            (i) => i.dictCode === 'bpmStatus'
          )[0].resourceDictItems
        }
      })
    },

    search() {
      this.parameter.pageIndex = 1
      this.getList()
    },

    pageChange(page) {
      this.parameter.pageIndex = page
      this.getList()
    },

    // 获取用户列表
    getList() {
      queryProjectList(this.parameter).then((res) => {
        if (res.success) {
          this.total = res.data.count
          this.projectList = res.data.list.map((item) => {
            return {
              ...item,
              // projectTypeText:
              //   this.projectTypeOptions.filter(
              //     (i) => i.value === item.projectType
              //   )[0]?.label,
              // projectSdjgText:
              //   this.projectStructOptions.filter(
              //     (i) => i.value === item.projectSdjg
              //   )[0]?.label,
              // xflxText: this.projectDivisionOptions.filter(
              //   (i) => i.value === item.xflx
              // )[0]?.label,
              // projectStatusText:
              //   this.projectStatusOptions.filter(
              //     (i) => i.value === item.projectStatus
              //   )[0]?.label,
              // bpmStatusText:
              //   this.projectCheckOptions.filter(
              //     (i) => i.value === item.bpmStatus
              //   )[0]?.label
              jszqText: item.jszq
                ? `${JSON.parse(item.jszq)[0]}  -  ${
                    JSON.parse(item.jszq)[1]
                  }`
                : ''
            }
          })
        }
      })
    },

    // 表格高亮
    tableRowClassName({ rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'light'
      } else {
        return ''
      }
    },

    addProject() {
      // this.$emit('addProject')
      this.$router.push({
        path: '/wisdomConstruction/messageManagement/form',
        query: {
          isView: false,
          isCheck: false,
          title: '添加'
        }
      })
    },

    // 刷新列表
    refresh() {
      this.parameter.pageIndex = 1
      this.parameter.projectType = ''
      this.parameter.projectSdjg = ''
      this.parameter.xflx = ''
      this.parameter.projectStatus = ''
      this.parameter.bpmStatus = ''
      this.parameter.keyword = ''
      this.parameter.sortFields = ''
      this.parameter.sortTypes = ''
      this.getList()
    },

    // 进度更新
    updateProgress(row) {
      // this.$emit('updateProgress', row)
      this.$router.push({
        path: '/wisdomConstruction/messageManagement/form',
        query: {
          pkid: row.pkid,
          isView: false,
          isCheck: false,
          title: '编辑'
        }
      })
    },

    // 项目审核
    checkProject(row) {
      this.$router.push({
        path: '/wisdomConstruction/messageManagement/form',
        query: {
          pkid: row.pkid,
          isView: true,
          isCheck: true,
          title: '审核'
        }
      })
      // this.$emit('checkProject', row)
    },

    // 项目查看
    viewProject(row) {
      // this.$emit('viewProject', row)
      this.$router.push({
        path: '/wisdomConstruction/messageManagement/form',
        query: {
          pkid: row.pkid,
          isView: true,
          isCheck: false,
          title: '查看'
        }
      })
    },

    // 删除项目
    async deleteProject(row) {
      let res = await queryJudgeExist(row.pkid, '')
      if (res.data) {
        this.$message.warning(
          '该项目已有安全报告或巡查记录，不可删除'
        )
        return
      }

      this.$confirm(
        '此操作将永久删除该项目，是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          deleteProjectInfo(row.pkid).then((res) => {
            if (res.success) {
              this.$message.success(res.data)
              if (
                this.total % 10 === 1 &&
                this.parameter.pageIndex ===
                  parseInt(this.total / 10) + 1 &&
                this.parameter.pageIndex !== 1
              ) {
                this.parameter.pageIndex -= 1
              }
              this.getList()
            } else {
              this.$message.error(res.message)
            }
          })
        })
        .catch((error) => {
          this.$message.info('已取消删除')
        })
    },

    // 退回项目
    backProject(data) {
      this.$refs.tuihui.changeDialog(data.pkid)
    },

    // 排序
    handleSort(column) {
      if (column.order === 'ascending') {
        this.parameter.sortFields = 'project_no'
        this.parameter.sortTypes = 'asc'
      } else if (column.order === 'descending') {
        this.parameter.sortFields = 'project_no'
        this.parameter.sortTypes = 'desc'
      } else {
        this.parameter.sortFields = ''
        this.parameter.sortTypes = ''
      }

      this.parameter.pageIndex = 1
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">
@import '~@/assets/css/table.scss';
</style>
